﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using School.Entities;
using System.Globalization;
using System.Collections;
namespace School.Pages.Bus
{
    public partial class Bus_Student : System.Web.UI.Page
    {
        private static SchoolEntities db = new SchoolEntities();
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((IQueryable<int>)Session["privilage"] != null)
            {
                var x = (IQueryable<int>)Session["privilage"];

                if (x.Contains(1202) == true)
                {
                    if (!this.Page.IsPostBack)
                        FillControls();
                    lbltestBus.Visible = false;
                }
                else
                    Response.Redirect("../organization/no_privelege.aspx");
            }
            else
                Response.Redirect("../organization/no_privelege.aspx");
        }
        private void FillControls()
        {
            ddlCampus.DataSource = School.Uniform.PayOrder.AllCampus();
            ddlCampus.DataBind();
        }
        private void ddlBind(DropDownList ddl)
        {
            ddl.Items.Clear();
            ddl.Items.Insert(0, new ListItem("اختيار", "0"));
            ddl.DataBind();
        }
        protected void ddlCertificate_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlCertificate.SelectedValue != "0")
            {
                ddlStadge.DataSource = School.Uniform.PayOrder.GetStadgeByCertificate(int.Parse(ddlCertificate.SelectedValue));
                ddlBind(ddlStadge);
                PLpayment.Visible = false;
            }
        }
        protected void ddlStadge_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlStadge.SelectedValue != "0")
            {
                ddlGrade.DataSource = School.Uniform.PayOrder.GetGradeByStadge(int.Parse(ddlStadge.SelectedValue));
                ddlBind(ddlGrade);
                PLpayment.Visible = false;
            }
        }
        protected void ddlCampus_TextChanged(object sender, EventArgs e)
        {
            if (ddlCampus.SelectedValue != "0")
            {

                ddlCertificate.DataSource = School.Uniform.PayOrder.GetCertificateByCamp(int.Parse(ddlCampus.SelectedValue));
                ddlBind(ddlCertificate);
                PLpayment.Visible = false;
            }
        }
        protected void ddlGrade_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlGrade.SelectedValue != "0")
            {
                ddlstudent.DataSource = School.Service.Bus.AllFullName_BUS_student(Convert.ToInt32(ddlCampus.SelectedValue), Convert.ToInt32(ddlCertificate.SelectedValue), Convert.ToInt32(ddlStadge.SelectedValue), int.Parse(ddlGrade.SelectedValue));
                ddlBind(ddlstudent);
                PLpayment.Visible = false;
            }
        }
        private void ddlBind(DropDownList ddl, object o)
        {
            ddl.DataSource = o;
            ddl.Items.Clear();
            ddl.Items.Insert(0, new ListItem("اختيار", "0"));
            ddl.DataBind();
        }



        protected void btnview_Click(object sender, EventArgs e)
        {
            gvsbBind();
        }

        private void gvsbBind()
        {
            CultureInfo SaCulture = new CultureInfo("ar-eg");
            DateTimeFormatInfo Form = SaCulture.DateTimeFormat;
            Form.ShortDatePattern = "dd/MM/yyyy";
            long Ex_id = School.Service.Bus.Expen_ID("200");
            var Vouchers = from v in db.Vvoucher_bus select v;
            Vouchers = Vouchers.Where(s => s.Expense_Id == Ex_id);
            if (txtorderNumber.Text.Length > 0)
            {
                int Nu = Convert.ToInt32(txtorderNumber.Text);
                Vouchers = Vouchers.Where(s => s.Number == Nu);
            }
            if (txtdate.Text.Length > 0)
            {
                DateTime da = Convert.ToDateTime(txtdate.Text, Form);
                Vouchers = Vouchers.Where(s => s.Date == da);
            }
            if (ddlstudent.SelectedIndex > 0)
            {
                int st = Convert.ToInt32(ddlstudent.SelectedValue);
                Vouchers = Vouchers.Where(s => s.Student_Id == st);
            }
            if (Vouchers.Count() != 0)
            {
                Vouchers = from v in Vouchers where !(db.SRV_BUS_Student.Any(a => a.Voucher_Id == v.Number)) select v;
                gvsb.DataSource = Vouchers.ToList();
                gvsb.DataBind();
            }
            else
            {
                gvsb.DataSource = null;
                gvsb.DataBind();
                lbltestBus.Text = "لا توجد بيانات";
                lbltestBus.Visible = true;
            }
            if (gvsb.Rows.Count != 0)
            {
                PLpayment.Visible = true;
            }
        }

        protected void gvsb_SelectedIndexChanged(object sender, EventArgs e)
        {
            //GridViewRow row


            int? bgoid = 0, bbackid = 0, stgoid = 0, stbackid = 0; int Number = 0, Payment_order_num = 0, Student_Id = 0;
            int index = gvsb.SelectedRow.RowIndex;
            bgoid = Convert.ToInt32(gvsb.SelectedDataKey.Values["Bus_Go_Id"]);
            if (bgoid == 0)
                bgoid = null;
            bbackid = Convert.ToInt32(gvsb.SelectedDataKey.Values["Bus_Return_Id"]);
            if (bbackid == 0)
                bbackid = null;
            stgoid = Convert.ToInt32(gvsb.SelectedDataKey.Values["Station_Go_Id"]);
            if (stgoid == 0)
                stgoid = null;
            stbackid = Convert.ToInt32(gvsb.SelectedDataKey.Values["Station_Return_Id"]);
            if (stbackid == 0)
                stbackid = null;
            Number = Convert.ToInt32(gvsb.DataKeys[index].Values["Number"].ToString());
            Payment_order_num = Convert.ToInt32(gvsb.DataKeys[index].Values["Payment_order_num"].ToString());
            Student_Id = Convert.ToInt32(gvsb.DataKeys[index].Values["Student_Id"].ToString());
            bool re = School.Service.Bus.Addstudent_to_bus(Student_Id, bgoid, stgoid, bbackid, stbackid, Number, Payment_order_num);
            if (re == true)
            {
                gvsbBind();
                lbltestBus.Text = "تم التسكين بنجاح";

            }
            else
            {
                lbltestBus.Text = "لم تتم عملية الاضافة..حاول مرة اخري";
            }
            lbltestBus.Visible = true;
        }

     
    }
}


